# -*- coding: utf-8 -*-

#Joao Alturas_6197
#2014_11_11

import csv
import sys
import numpy as np
import matplotlib.pyplot as plt
import xlrd
from pylab import *
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from sqlalchemy import Column, Integer, String, Float, func
from sqlalchemy.ext.declarative import declarative_base

engine = create_engine('sqlite:///treta1.db')
Base = declarative_base()

"""
#Create base dados 
"""
class Dados(Base):
	
	__tablename__ = 'dados'	
	id = Column(Integer, primary_key=True)
	ano = Column(Integer)
	ipcGlobal = Column(Float) 	 
	varAnualTaxInfl = Column(Float)     
	remMinMensal = Column(Float)   
	remMaxMensal = Column(Float) 
	pibPercapitAnual = Column(Float) 
	rendNacBrutPercAnual = Column (Float)  
	rendDispBrutPercAnual = Column (Float)  
	remunPercapitAnual = Column (Float) 
	
	def __init__(self,ano,ipcGlobal,varAnualTaxInfl,remMinMensal,remMaxMensal,
		pibPercapitAnual,rendNacBrutPercAnual,rendDispBrutPercAnual,remunPercapitAnual):
											 
			self.ano = ano
			try:
				self.ipcGlobal = float(ipcGlobal)	
			except:
				self.ipcGlobal = 0
			try: 
				self.varAnualTaxInfl = float(varAnualTaxInfl)
			except:
				self.varAnualTaxInfl = 0
			try:
				self.remMinMensal = float(remMinMensal)
			except:
				self.remMinMensal = 0
			try:
				self.remMaxMensal = float(remMaxMensal)
			except:
				self.remMaxMensal= 0
			try:
				self.pibPercapitAnual = float(pibPercapitAnual)
			except:
				self.pibPercapitAnual = 0
			try:
				self.rendNacBrutPercAnual = float(rendNacBrutPercAnual)
			except:
				self.rendNacBrutPercAnual = 0
			try:
				self.rendDispBrutPercAnual = float(rendDispBrutPercAnual)
			except:
				self.rendDispBrutPercAnual = 0
			try:
				self.remunPercapitAnual = float(remunPercapitAnual)
			except:
				self.remunPercapitAnual = 0
				
         
Base.metadata.create_all(engine)
Session = sessionmaker(bind=engine)
session = Session()

"""
#Para Remuneração Percapita Anual
	
"""
dados = session.query(Dados.ano, Dados.remunPercapitAnual).all()
anos = session.query(Dados.ano).all()
limites = session.query(func.max(Dados.remunPercapitAnual).label("maximo"), func.min(Dados.remunPercapitAnual).label("minimo")).one()
remPercAnual = session.query(Dados.remMaxMensal).all()
print len(anos), len(remPercAnual)	
anos =[x[0] for x in anos]
anos.pop(0)
anos = [0] + anos
plot(range(1977,1977+len(remPercAnual)),remPercAnual)
xlabel(u'Anos')
ylabel(u'Remuneração Percapita Anual')
#plot(x,y,'rd')
grid()
title(u'Remuneração Percapita Anual')
show()

